package com.blackline.auto.Report;

import java.util.Iterator;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.apache.log4j.Logger;
import org.junit.*;
import static org.junit.Assert.*;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.interactions.Actions;

import com.blackline.auto.common.CommonMethods;
import com.blackline.declareUI.UI;
//import com.newb.AutoDriver.AutoDriver;
import com.blackline.parameters.*;

/**
 * @author XBO
 * @version Feb, 2013
 * <p>The smoke test to Reports module.
 */
public class Reports_smoketest {
	private WebDriver driver;
	private StringBuffer verificationErrors = new StringBuffer();
	private static Logger logger=Logger.getLogger(Reports_smoketest.class);
	UI UI=new UI();
	Parameters parameters;
	@Before
	public void setUp() throws Exception {
		FirefoxProfile profile=new FirefoxProfile();
		profile.setEnableNativeEvents(true);
		parameters=new Parameters("parameters.xml");
		driver=new FirefoxDriver();
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		driver.manage().window().maximize();
		UI.declareAllUI(driver);
	}

	@Test
	public void Reportssmoketest() throws Exception {
		int sleepTime = 5000;
		UI.LogonPage.Login(driver, parameters.urlDev4, parameters.getLogonID("admin"), parameters.getPassword("admin"));
		Thread.sleep(3000);
		UI.NavigationMenu.REPORTS.click();
		UI.Reports.Enhanced_Reporting_link.click();
		Thread.sleep(20000);
		//UI.Reporting.BlackLine_Reports_nodeInLeftTree.click();
		UI.Reporting.AddIcon_BlackLineReports.click();
		Thread.sleep(20000);
		UI.Reporting.AddIcon_Account.click();
		Thread.sleep(20000);
		UI.Reporting.Information_nodeInLeftTree.click();
		Thread.sleep(sleepTime + 3);
		UI.Reporting.Name_text_field.sendKeys("User Access");
		UI.Reporting.Refresh_button.click();
		Thread.sleep(sleepTime + 5000);
		UI.Reporting.Run_linkOfSearchedResult.click();
		UI.ReportPrompts.Run_Report_button.click();
		Thread.sleep(sleepTime);
		String runRes=UI.Alert_inReporting.Message_Text.getText();
		if(runRes.contains("being processed and will be available"))
		  {
			  logger.info("[Pass!] "+runRes);
			  UI.Alert_inReporting.OK_button.click();
		  }
		  else{
			  logger.error("[Fail!] "+runRes);
			  verificationErrors.append("[Fail!] "+runRes);	 
			  fail("Run failed: "+runRes);
		  }
		  Thread.sleep(sleepTime);
		UI.Reporting.Report_Log_nodeInLeftTree.click();
		Thread.sleep(sleepTime + 10000);
		//Wait for the status to change from Running to Ready
//		UI.Reporting.Name_text_field.sendKeys("User Access");
////	UI.Reporting.Refresh_button.click();
//		 UI.Reporting.Name_text_field.sendKeys(Keys.ENTER);
		 logger.info("after clicking reportlog node...");
		 Thread.sleep(sleepTime);
		 String sReportName="",sMethod="",sStatus="";
		 int s=0;
		 while(true){
			 if(s>=60){break;}
			 s++;
			 //Get report run
			 int i=1,j=1;
			 List<WebElement> odd=UI.Reporting.Report_LogOdd;
			 Iterator<WebElement> iOdd=odd.iterator();
			 while(iOdd.hasNext()){
				 WebElement a=iOdd.next();
				 //logger.info("i="+i);
				 if(a.getText().equals("User Access")){
					 break;
				 }
				 i++;
			 }
			 List<WebElement> even=UI.Reporting.Report_LogEven;
			 Iterator<WebElement> iEven=even.iterator();
			 while(iEven.hasNext()){
				 WebElement b=iEven.next();
				 //logger.info("------------j="+j);
				 if(b.getText().equals("User Access")){
					 break;
				 }
				 j++;
			 }
			 int n=i<=j?i:j;
			 WebElement reportName=i<=j?driver.findElement(By.cssSelector(".webControls_BlackLineGrid_RowOdd:nth-child("+n+") td:nth-child(4) ")):driver.findElement(By.cssSelector(".webControls_BlackLineGrid_RowEven:nth-child("+n+") td:nth-child(4) "));
			 WebElement method=i<=j?driver.findElement(By.cssSelector(".webControls_BlackLineGrid_RowOdd:nth-child("+n+") td:nth-child(5) ")):driver.findElement(By.cssSelector(".webControls_BlackLineGrid_RowEven:nth-child("+n+") td:nth-child(5) "));
			 WebElement status=i<=j?driver.findElement(By.cssSelector(".webControls_BlackLineGrid_RowOdd:nth-child("+n+") td:nth-child(6) ")):driver.findElement(By.cssSelector(".webControls_BlackLineGrid_RowEven:nth-child("+n+") td:nth-child(6) "));
			 sReportName=reportName.getText();
			 sMethod=method.getText();
			 sStatus=status.getText();
			 logger.info("Current status is "+sStatus);
			 if(!(sStatus.equals("Pending")||sStatus.equals("Running"))){break;}
			 
			 Thread.sleep(10000);
			 UI.Reporting.Refresh_button.click();
			 Thread.sleep(sleepTime);
			 logger.info("Waiting... ... "+10*s+" seconds...");
		  }
		  logger.info("report="+sReportName+", method="+sMethod+", status="+sStatus);
		  if ("Ready".equals(sStatus)&&sMethod.equals("On Demand"))
		  {
			  logger.info("[Pass!] Status is as expectation: " + sStatus+", Method is as expectation: " + sMethod);
		  }
		  else
		  {
			  logger.info("[Fail!] Status should be 'Ready', now is: " + sStatus+", Method should be 'On Demand', now is: " + sMethod);
			  verificationErrors.append("[Fail!] Status should be 'Ready', now is: " + sStatus+", Method should be 'On Demand', now is: " + sMethod);
		  }
	}

	@After
	public void tearDown() throws Exception {
		CommonMethods.takeScreenShot(this.getClass(), driver);
		driver.quit();
		String verificationErrorString = verificationErrors.toString();
		if (!"".equals(verificationErrorString)) {
			fail(verificationErrorString);
		}
	}
}
